// pages/recruit/introduction.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
      idOfPrg:0,
      flag:0
    },
    gotoApplication() {
      this.setData({
        userInfo: wx.getStorageSync('userInfo'), // 用户的微信头像、昵称等信息
        loginFlag: wx.getStorageSync('loginFlag'), // 判断是否登录，登录为1，未登录为0或空
        sessionID: wx.getStorageSync('sessionID'),
        chuangNum: wx.getStorageSync('chuangNum')
      })
      if (this.data.loginFlag != 1) {
        this.myShowModal();
    }
    console.log("status",this.data.status,"flag",this.data.loginFlag)
    if(this.data.status!=-1&&this.data.loginFlag==1){
      wx.navigateTo({
        url: '/pages/recruit/application/application?idOfPrg='+this.data.idOfPrg,
      })  
    } else if(this.data.status==-1&&this.data.loginFlag==1){
      wx.showToast({
        title: '项目已超出招募时间',
        icon:'loading',
        duration:2000
      })
      wx.navigateBack({
        delta: 1,
      })
    }
      },
      myShowModal() {
        wx.showModal({
            title: '提示',
            content: '登录后查看更多，是否登录？',
            success: (res) => {
                // 确定
                if (res.confirm) {
                    this.logIn();
                }
                else {
                     wx.showToast({
                         title: '跳转到首页',
                         icon: 'none',
                        duration: 1500
                     })
                     setTimeout(function () {
                      wx.switchTab({
                        url: '/pages/recruit/recruit',
                        success:function(e)
                        {
                          var page = getCurrentPages().pop();
                          if (page == undefined || page == null) return;
                          page.onLoad();
                        }
                      })  
                    }, 1500)
                }
            }
        })

      },
    
      logIn() {
        // 如果userInfo为空，则未确认过微信授权弹窗，即首次登录
        if (!this.data.userInfo) {
            // 微信授权弹窗
            wx.getUserProfile({
                desc: '用于获取用户头像和昵称',
                // 用户点击确认
                success: (res) => {
                    let userInfo = res.userInfo;
                    wx.setStorageSync('userInfo', userInfo); // 缓存userInfo
                    this.myLogin(); // 进行登录
                },
                // 用户点击取消
                fail: (res) => {
                    // wx.showToast({
                    //     title: '跳转到首页',
                    //     icon: 'none',
                    //     duration: 1500
                    // })
                }
            })
        }
        // 非首次登录
        else {
            this.myLogin(); // 重新登录
        }
    },
    
    // wx.login()封装
    myLogin() {
        wx.login({
            success: (res) => {
                wx.request({
                    url: 'https://www.hschuangsai.com/user/login/' + res.code,
                    method: 'GET',
                    success: (res) => {
                        console.log("myLogin success", res);
                        let sessionID = res.data.data.sessionId;
                        wx.setStorageSync('chuangNum',res.data.data.chuangNum)
                        wx.setStorageSync('sessionID', sessionID); // 缓存sessionID
                        wx.setStorageSync('loginFlag', 1); // 缓存loginFlag
                        this.setData({
                            sessionID: sessionID,
                            loginFlag: 1,
                        });
                          this.gotoApplication()
                    }
                })
            }
        })
    }, 
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      this.setData({
        idOfPrg:options.idOfPrg,
        membersOfPrg:[],
        briefDemand:'',
        captainName:'',
        endTime:'',
        institute:'',
        name:'',
        planEndTime:'',
        planStartTime:'',
        recruitNum:0,
        startTime:'',
        teacherApartment:'',
        teacherName:'',
        teacherRank:'',
        memberName:'',
        memberTelephone:'',
        memberMajor:'',
        memberWork:'',
        status:-1,
        flag:0,
        chuangNum: wx.getStorageSync('chuangNum')
      })
      this.setData({
        userInfo: wx.getStorageSync('userInfo'), // 用户的微信头像、昵称等信息
        loginFlag: wx.getStorageSync('loginFlag'), // 判断是否登录，登录为1，未登录为0或空
        sessionID: wx.getStorageSync('sessionID'),
        chuangNum: wx.getStorageSync('chuangNum'),
      })
    if (this.data.loginFlag != 1) {
        this.myShowModal();
    }
      wx.request({
        url:'https://www.hschuangsai.com/recruitProject/info?projectId='+this.data.idOfPrg,
        method:"GET",
        success:(res)=>{
          console.log(res)
          this.setData({
            membersOfPrg:res.data.data.members,
            briefDemand:res.data.data.recruitProject.briefDemand,
            captainName:res.data.data.recruitProject.captainName,
            DemandHtml:res.data.data.recruitProject.demand,
            endTime:res.data.data.recruitProject.endTime,
            institute:res.data.data.recruitProject.institute,
            IntroHtml:res.data.data.recruitProject.introduction,
            name:res.data.data.recruitProject.name,
            planEndTime:res.data.data.recruitProject.planEndTime,
            planStartTime:res.data.data.recruitProject.planStartTime,
            recruitNum:res.data.data.recruitProject.recruitNum,
            startTime:res.data.data.recruitProject.startTime,
            teacherApartment:res.data.data.recruitProject.teacherApartment,
            teacherName:res.data.data.recruitProject.teacherName,
            teacherRank:res.data.data.recruitProject.teacherRank,
            status:res.data.data.recruitProject.status,
          })
          for (var i=0;i<this.data.membersOfPrg.length;i++)
          {
            if (this.data.membersOfPrg[i].chuangNum==this.data.chuangNum)
            {
              this.setData({
                flag:1
              })
            }
          }
          console.log(res.data)

        }
      })
  },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function (options) {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
      wx.switchTab({
        url: '/pages/recruit/recruit',
        success:function(e)
        {
          var page = getCurrentPages().pop();
          if (page == undefined || page == null) return;
          page.onLoad();
        }
      })  
    },


    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})

   